Skip to content

Add EValue::tryTo<T>() for all EValue payload types#19036

Closed
lucylq wants to merge 1 commit into
mainfrom
gh/lucylq/149/head
Closed

Add EValue::tryTo<T>() for all EValue payload types#19036
lucylq wants to merge 1 commit into
mainfrom
gh/lucylq/149/head

Conversation

@lucylq
Copy link
Copy Markdown
Contributor

@lucylq lucylq commented Apr 21, 2026

Introduce tryTo versions of each Evalue accessor.

Current accessors toTensor(), etc. abort with ET_CHECK_MSG when it's an incorrect type.

API additions:

  • Per-type: tryToInt, tryToDouble, tryToBool, tryToScalar, tryToString,
    tryToTensor (already present, kept), tryToIntList, tryToBoolList,
    tryToDoubleList, tryToTensorList, tryToListOptionalTensor,
    tryToScalarType, tryToMemoryFormat, tryToLayout, tryToDevice.
    Tag mismatch returns Error::InvalidType; null list/string payload
    returns Error::InvalidState.
  • Templated tryTo() dispatcher mirroring to(), via a new
    EVALUE_DEFINE_TRY_TO macro kept adjacent to EVALUE_DEFINE_TO so drift
    between the two surfaces is visible at review time.
  • tryToOptional() widened from Tensor-only to generic, delegating
    to tryTo() so it works for any supported payload type.

Tests cover success + mismatch paths for each new accessor, plus the
widened tryToOptional() path.

Authored-with: Claude

[ghstack-poisoned]
@lucylq
Copy link
Copy Markdown
Contributor Author

lucylq commented Apr 21, 2026

Copilot AI review requested due to automatic review settings April 21, 2026 23:56
@lucylq lucylq requested a review from JacobSzwejbka as a code owner April 21, 2026 23:56
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Apr 21, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19036

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 133 Pending

As of commit 6e4eab1 with merge base 2d53535 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 21, 2026
@lucylq lucylq mentioned this pull request Apr 21, 2026
@github-actions
Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds non-aborting, Result-returning “tryTo*” accessors to executorch::runtime::EValue so callers can safely handle tag mismatches (and certain invalid payload states) without terminating the process—especially important when processing untrusted .pte inputs.

Changes:

  • Added Result-returning tryTo* accessors for EValue payload types, plus a templated tryTo<T>() dispatcher.
  • Generalized tryToOptional<T>() beyond Tensor to work with any supported payload type via tryTo<T>().
  • Added unit tests covering success/mismatch paths for several new accessors and the widened tryToOptional<T>().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
runtime/core/evalue.h Introduces tryTo* APIs, tryTo<T>() specializations, and generic tryToOptional<T>().
runtime/core/test/evalue_test.cpp Adds test coverage for new tryTo* APIs and the generalized tryToOptional<T>().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +563 to +602
// List tryTo* — mismatch paths. Success paths require building a
// BoxedEvalueList or ArrayRef host object, which the non-list tests above
// already cover via the shared tag-check logic; one mismatch test per list
// type is enough to exercise the added code.

TEST_F(EValueTest, TryToIntListTypeMismatch) {
EValue e(static_cast<int64_t>(42));
auto result = e.tryToIntList();
EXPECT_EQ(result.error(), executorch::runtime::Error::InvalidType);
}

TEST_F(EValueTest, TryToDoubleListTypeMismatch) {
EValue e(static_cast<int64_t>(42));
auto result = e.tryToDoubleList();
EXPECT_EQ(result.error(), executorch::runtime::Error::InvalidType);
}

TEST_F(EValueTest, TryToBoolListTypeMismatch) {
EValue e(static_cast<int64_t>(42));
auto result = e.tryToBoolList();
EXPECT_EQ(result.error(), executorch::runtime::Error::InvalidType);
}

TEST_F(EValueTest, TryToTensorListTypeMismatch) {
EValue e(static_cast<int64_t>(42));
auto result = e.tryToTensorList();
EXPECT_EQ(result.error(), executorch::runtime::Error::InvalidType);
}

TEST_F(EValueTest, TryToListOptionalTensorTypeMismatch) {
EValue e(static_cast<int64_t>(42));
auto result = e.tryToListOptionalTensor();
EXPECT_EQ(result.error(), executorch::runtime::Error::InvalidType);
}

TEST_F(EValueTest, TryToStringTypeMismatch) {
EValue e(static_cast<int64_t>(42));
auto result = e.tryToString();
EXPECT_EQ(result.error(), executorch::runtime::Error::InvalidType);
}
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new tryToString()/tryTo*List() accessors add an Error::InvalidState path for null internal payload pointers, but the added tests only cover tag-mismatch for string/lists (and no success case for tryToString). Consider adding explicit tests that (1) a well-formed String/List returns ok(), and (2) a manually-constructed EValue with the right tag but a null payload pointer returns Error::InvalidState (rather than aborting).

Copilot uses AI. Check for mistakes.
Comment thread runtime/core/evalue.h
Comment on lines +343 to +349
Result<executorch::aten::Tensor> tryToTensor() const {
if (!isTensor()) {
return Error::InvalidType;
}
return payload.as_tensor;
}

Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tryToTensor() returns a Tensor by value from a const EValue, which will typically incur a Tensor copy/refcount bump even when the caller only needs to inspect the tensor. If the goal is a safe, non-aborting accessor for performance-sensitive paths, consider returning a pointer type (e.g., Result<const Tensor*>) or adding an rvalue overload (tryToTensor() &&) that can move out, to avoid extra copies where possible.

Suggested change
Result<executorch::aten::Tensor> tryToTensor() const {
if (!isTensor()) {
return Error::InvalidType;
}
return payload.as_tensor;
}
Result<const executorch::aten::Tensor*> tryToTensor() const& {
if (!isTensor()) {
return Error::InvalidType;
}
return &payload.as_tensor;
}
Result<executorch::aten::Tensor> tryToTensor() && {
if (!isTensor()) {
return Error::InvalidType;
}
auto res = std::move(payload.as_tensor);
clearToNone();
return res;
}

Copilot uses AI. Check for mistakes.
Comment thread runtime/core/evalue.h
Comment on lines +625 to +639
/**
* Result-returning equivalent of `toOptional<T>()`. None maps to an empty
* optional; any other tag that doesn't match T propagates `tryTo<T>()`'s
* error (`Error::InvalidType`).
*/
template <typename T>
inline Result<std::optional<T>> tryToOptional() const {
if (this->isNone()) {
return std::optional<T>(executorch::aten::nullopt);
}
auto r = this->tryTo<T>();
if (!r.ok()) {
return r.error();
}
return std::optional<T>(std::move(r.get()));
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring for tryToOptional() says it propagates tryTo()'s error (Error::InvalidType), but tryTo() can also return other errors (e.g., tryToString()/tryTo*List() return Error::InvalidState on null payload pointers). Consider updating the comment to avoid implying InvalidType is the only possible error.

Copilot uses AI. Check for mistakes.
Comment thread runtime/core/evalue.h
tryToTensorList)
EVALUE_DEFINE_TRY_TO(
executorch::aten::ArrayRef<std::optional<executorch::aten::Tensor>>,
tryToListOptionalTensor)
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tryTo() is described as mirroring to(), but the tryTo dispatcher only specializes the non-optional payload types. to() also supports std::optional<...> specializations (e.g., std::optional, std::optional<ArrayRef<int64_t>>, etc.), so tryTo<std::optional<...>>() will currently be an undefined/unspecialized template and fail at link-time. Consider adding EVALUE_DEFINE_TRY_TO specializations for the same optional-returning types, or explicitly deleting/guarding unsupported instantiations to produce a clear compile-time error and steer users to tryToOptional().

Suggested change
tryToListOptionalTensor)
tryToListOptionalTensor)
#define EVALUE_DEFINE_TRY_TO_OPTIONAL(cpp_type) \
template <> \
inline Result<std::optional<cpp_type>> EValue::tryTo< \
std::optional<cpp_type>>() const { \
return tryToOptional<cpp_type>(); \
}
EVALUE_DEFINE_TRY_TO_OPTIONAL(executorch::aten::Tensor)
EVALUE_DEFINE_TRY_TO_OPTIONAL(executorch::aten::ArrayRef<int64_t>)
EVALUE_DEFINE_TRY_TO_OPTIONAL(executorch::aten::ArrayRef<double>)
EVALUE_DEFINE_TRY_TO_OPTIONAL(executorch::aten::ArrayRef<bool>)
EVALUE_DEFINE_TRY_TO_OPTIONAL(
executorch::aten::ArrayRef<executorch::aten::Tensor>)
EVALUE_DEFINE_TRY_TO_OPTIONAL(
executorch::aten::ArrayRef<std::optional<executorch::aten::Tensor>>)
#undef EVALUE_DEFINE_TRY_TO_OPTIONAL

Copilot uses AI. Check for mistakes.
@lucylq lucylq closed this Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. security-fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants